using System;
using System.Web.UI.WebControls;
using NET.Library.Types;

namespace NET.Library.Web.Forms
{
    /// <summary>
    /// Class Name : Values
    /// Author     : Martin Tomkins
    /// Synopsis   : Class used for returning typed values from web controls.
    /// </summary>
    public class Values : Strings
    {
        #region TextBox Controls
        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="textBox"></param>
        /// <returns>Returns integer value from TextBox value in a web form</returns>
        /// '-----------------------------------------------------------------------------------------
        public static int IntTextValue(TextBox textBox)
        {
            return (!IsEmptyString(textBox.Text)) ? Convert.ToInt32(RemoveInvalidChar(textBox.Text)) : 0;
        }

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns string value from TextBox value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static string StringTextValue(TextBox textBox)
        {
            return (!IsEmptyString(textBox.Text)) ? RemoveInvalidChar(textBox.Text) : String.Empty;
        }

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns DateTime value from TextBox value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static DateTime DateTimeTextValue(TextBox textBox)
        {
            return (!IsEmptyString(textBox.Text)) ? Convert.ToDateTime(RemoveInvalidChar(textBox.Text)) : DateTimes.MinDateTime;
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert TextBox text in a web form to a Single value
        /// </summary>
        /// <param name="textBox"></param>
        /// <returns>Single</returns>
        /// '-----------------------------------------------------------------------------------------
        public static Single SingleTextValue(TextBox textBox)
        {
            return (!IsEmptyString(textBox.Text)) ? Convert.ToSingle(RemoveInvalidChar(textBox.Text)) : Single.MinValue;
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert TextBox text in a web form to a decimal value
        /// </summary>
        /// <param name="textBox"></param>
        /// <returns></returns>
        /// '-----------------------------------------------------------------------------------------
        public static decimal DecimalTextValue(TextBox textBox)
        {
            return (!IsEmptyString(textBox.Text)) ? Convert.ToDecimal(RemoveInvalidChar(textBox.Text)) : decimal.Zero;
        }
        #endregion

        #region HiddenField Controls
        public static int IntTextValue(HiddenField hiddenField)
        {
            return (!IsEmptyString(hiddenField.Value)) ? Convert.ToInt32(RemoveInvalidChar(hiddenField.Value)) : 0;
        }

        public static string StringTextValue(HiddenField hiddenField)
        {
            return (!IsEmptyString(hiddenField.Value)) ? RemoveInvalidChar(hiddenField.Value) : String.Empty;
        }

        public static DateTime DateTimeTextValue(HiddenField hiddenField)
        {
            return (!IsEmptyString(hiddenField.Value)) ? Convert.ToDateTime(RemoveInvalidChar(hiddenField.Value)) : DateTimes.MinDateTime;
        }

        public static Single SingleTextValue(HiddenField hiddenField)
        {
            return (!IsEmptyString(hiddenField.Value)) ? Convert.ToSingle(RemoveInvalidChar(hiddenField.Value)) : Single.MinValue;
        }

        public static decimal DecimalTextValue(HiddenField hiddenField)
        {
            return (!IsEmptyString(hiddenField.Value)) ? Convert.ToDecimal(RemoveInvalidChar(hiddenField.Value)) : decimal.Zero;
        }
        #endregion

        #region Label Controls
        /// '-----------------------------------------------------------------------------------------
        /// 'Returns integer value from Label value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static int IntTextValue(Label label)
        {
            return (!IsEmptyString(label.Text)) ? Convert.ToInt32(RemoveInvalidChar(label.Text)) : 0;
        }

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns string value from Label value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static string StringTextValue(Label label)
        {
            return (!IsEmptyString(label.Text)) ? RemoveInvalidChar(label.Text) : String.Empty;
        }

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns DateTime value from Label value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static DateTime DateTimeTextValue(Label label)
        {
            return (!IsEmptyString(label.Text)) ? Convert.ToDateTime(RemoveInvalidChar(label.Text)) : DateTimes.MinDateTime;
        }
        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert Label text in a web form to a Single value
        /// </summary>
        /// <param name="label"></param>
        /// <returns>Single</returns>
        /// '-----------------------------------------------------------------------------------------
        public static Single SingleTextValue(Label label)
        {
            return (!IsEmptyString(label.Text)) ? Convert.ToSingle(RemoveInvalidChar(label.Text)) : Single.MinValue;
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert Label text in a web form to a decimal value
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        /// '-----------------------------------------------------------------------------------------
        public static decimal DecimalTextValue(Label label)
        {
            return (!IsEmptyString(label.Text)) ? Convert.ToDecimal(RemoveInvalidChar(label.Text)) : decimal.Zero;
        }
        #endregion

        #region Literal Controls
        /// '-----------------------------------------------------------------------------------------
        /// 'Returns string value from Literal value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static string StringTextValue(Literal literal)
        {
            return (!IsEmptyString(literal.Text)) ? RemoveInvalidChar(literal.Text) : String.Empty;
        }

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns integet value from Literal value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static int IntTextValue(Literal literal)
        {
            return (!IsEmptyString(literal.Text)) ? Convert.ToInt32(RemoveInvalidChar(literal.Text)) : 0;
        }

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns DateTime value from Literal value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static DateTime DatetimeTextValue(Literal literal)
        {
            return (!IsEmptyString(literal.Text)) ? Convert.ToDateTime(RemoveInvalidChar(literal.Text)) : DateTimes.MinDateTime;
        }
        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert Literal text in a web form to a Single value
        /// </summary>
        /// <param name="literal"></param>
        /// <returns>Single</returns>
        /// '-----------------------------------------------------------------------------------------
        public static Single SingleTextValue(Literal literal)
        {
            return (!IsEmptyString(literal.Text)) ? Convert.ToSingle(RemoveInvalidChar(literal.Text)) : Single.MinValue;
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert Literal text in a web form to a decimal value
        /// </summary>
        /// <param name="literal"></param>
        /// <returns></returns>
        /// '-----------------------------------------------------------------------------------------
        public static decimal DecimalTextValue(Literal literal)
        {
            return (!IsEmptyString(literal.Text)) ? Convert.ToDecimal(RemoveInvalidChar(literal.Text)) : decimal.Zero;
        }
        #endregion

        #region CheckBox Controls
        /// '-----------------------------------------------------------------------------------------
        /// 'Returns integer value for checkbox fields in a web form
        /// '-----------------------------------------------------------------------------------------
        public static int IntCheckBoxValue(CheckBox checkBox)
        {
            return checkBox.Checked == true ? 1 : 0;
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Return Y or N for checkbox fields in a web form
        /// </summary>
        /// <param name="checkBox"></param>
        /// <returns>Y if checked, N if not.</returns>
        /// '-----------------------------------------------------------------------------------------
        public static string StringCheckBoxValue(CheckBox checkBox)
        {
            return checkBox.Checked == true ? "Y" : "N";
        }
        #endregion

        // '-----------------------------------------------------------------------------------------
        // 'Returns integer value for checkboxlist fields
        // '-----------------------------------------------------------------------------------------
        //		public static int intChkboxlsVal(CheckBoxList CheckBoxList, int Item)
        //		{
        ////			foreach (int i in CheckBoxList.Items)
        ////			{
        ////				if CheckBoxList.Items(i).s
        ////			}
        //
        //			if (CheckBoxList.Items.Count .Contains( Items.Item(Item).Selected = true))
        //			{
        //				return 1;
        //			}
        //			else
        //			{
        //				return 2;
        //			}
        //		}

        #region RadioButtonList Controls
        /// '-----------------------------------------------------------------------------------------
        /// 'Returns integer value from radiobuttonlist selected value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static int IntRadioListValue(RadioButtonList radioButtonList)
        {
            return radioButtonList.SelectedItem != null ? Convert.ToInt32(radioButtonList.SelectedItem.Value) : 0;
        }

        public static void SelectRadioButtonItems(RadioButtonList radioButtonList, string selectionValue)
        {
            string itemValue = "";
            int listCount = radioButtonList.Items.Count;

            for (int i = 0; i < listCount; i++)
            {
                itemValue = radioButtonList.Items[i].Value;

                if (selectionValue.Equals(itemValue))
                {
                    radioButtonList.Items[i].Selected = true;
                }
            }
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Check if an item in a RadioButtonList is selcted.
        /// </summary>
        /// <param name="radioButtonList"></param>
        /// <returns>True if 1 or 0 is the selected value, else false.</returns>
        /// '-----------------------------------------------------------------------------------------
        public static bool IsRadioButtonSelected(RadioButtonList radioButtonList)
        {
            return !(IntRadioListValue(radioButtonList) != 1 && IntRadioListValue(radioButtonList) != 0);
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Return a boolean value based on the value of the selected item in the RadioButtonList.
        /// </summary>
        /// <param name="radioButtonList"></param>
        /// <returns>True if the selected value is 1, else false.</returns>
        /// '-----------------------------------------------------------------------------------------
        public static bool RadioButtonSelectedValueAsBoolean(RadioButtonList radioButtonList)
        {
            return IntRadioListValue(radioButtonList).Equals(1) ? true : false;
        }
        #endregion

        #region DropDownList Controls
        /// '-----------------------------------------------------------------------------------------
        /// 'Returns string value from DropDownList selected value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static string StringDropDownListValue(DropDownList dropDownList)
        {
            return dropDownList.SelectedItem != null ? dropDownList.SelectedItem.Value : String.Empty;
        }

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns integer value from DropDownList selected value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static int IntDropDownListValue(DropDownList dropDownList)
        {
            return dropDownList.SelectedItem != null ? Convert.ToInt32(dropDownList.SelectedItem.Value) : 0;
        }

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns string value from DropDownList selected text in a web form
        /// '-----------------------------------------------------------------------------------------
        public static string StringDropDownListText(DropDownList dropDownList)
        {
            return dropDownList.SelectedItem != null ? RemoveInvalidChar(dropDownList.SelectedItem.Text) : String.Empty;
        }

        /// -----------------------------------------------------------------------------------------
        /// <summary>
        /// Pre-select an item in a DropDownList control by a value
        /// </summary>
        /// <param name="dropDownList"></param>
        /// <param name="valueToSelect"></param>
        /// -----------------------------------------------------------------------------------------
        public static void SetDropDownListSelectedValue(DropDownList dropDownList, string valueToSelect)
        {
            for (int i = 0; i < dropDownList.Items.Count; i++)
            {
                if (dropDownList.Items[i].Value == valueToSelect)
                {
                    dropDownList.SelectedIndex = i;
                    break;
                }
            }
        }

        /// -----------------------------------------------------------------------------------------
        /// <summary>
        /// Pre-select an item in a DropDownList control by text
        /// </summary>
        /// <param name="dropDownList"></param>
        /// <param name="textToSelect"></param>
        /// -----------------------------------------------------------------------------------------
        public static void SetDropDownListSelectedText(DropDownList dropDownList, string textToSelect)
        {
            //if (Validation.IsNumeric(textToSelect))
            //{
            for (int i = 0; i < dropDownList.Items.Count; i++)
            {
                if (dropDownList.Items[i].Text == textToSelect)
                {
                    dropDownList.SelectedIndex = i;
                    break;
                }
            }
            //}
        }
        #endregion
    }
}

